package com.guige.base.util;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComFailException;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

import java.util.Date;

/**
 * TODO
 *
 * @author songaw
 * @date 2018/5/17 9:54
 */
public class JacobUtil {
    //word转PDF
    private static final String wdFormatPDF="17";
    //html转xls
    private static final String htmlFormatExcel="51";
    //PPT 转pdf
    private static final String pptFormatPDF="32";
    //excel转PDF
    private static final String xlTypePDF="0";

    /**
     * html转excel
     * @param srcFilePath
     * @param targetFilePath
     * @return
     */
    public static boolean htmlToExcel(String srcFilePath, String targetFilePath) {
        ActiveXComponent app = null;
        Dispatch excel = null;
        try {
             app = new ActiveXComponent("Excel.Application");
            System.out.println("*****正在转换...*****");
            // 设置Excel应用程序不可见
            app.setProperty("Visible", false);
            // Workbooks表示Excel程序的所有文档窗口，（
            app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
            Dispatch excels = app.getProperty("Workbooks").toDispatch();
            //打开要转换的Excel文件
            excel = Dispatch.call(excels, "Open", srcFilePath, false,
                    true).toDispatch();
            //设置兼容性检查为false
            Dispatch.put(excel, "CheckCompatibility", false);
            app.setProperty("DisplayAlerts",false);
            Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[]{targetFilePath, new Variant(htmlFormatExcel)}, new int[1]);
            app.setProperty("DisplayAlerts",true);
            System.out.println("*****转换成功...*****");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (excel != null) {
                Dispatch.call(excel, "Close", false);
            }
            if (app != null) {
                app.invoke("Quit", new Variant[] {});
                //app.invoke("Quit", 0);
            }
            ComThread.Release();
        }
    }

    /**
     * word转pdf
     * @param srcFilePath
     * @param targetFilePath
     * @return
     */
    public static boolean docToPdf(String srcFilePath, String targetFilePath) {
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            ComThread.InitSTA();
            app = new ActiveXComponent("Word.Application");
            System.out.println("*****正在转换...*****");
            app.setProperty("Visible", false);
            Dispatch docs = app.getProperty("Documents").toDispatch();
            doc = Dispatch.invoke(docs, "Open", Dispatch.Method,
                    new Object[] { srcFilePath,
                            new Variant(false),
                            new Variant(true),//是否只读
                            new Variant(false),
                            new Variant("pwd") },
                    new int[1]).toDispatch();
             //Dispatch.put(doc, "CheckCompatibility", false);  //兼容性检查,为特定值false不正确
            Dispatch.put(doc, "RemovePersonalInformation", false);
            app.setProperty("DisplayAlerts",false);
            Dispatch.call(doc, "ExportAsFixedFormat", targetFilePath, wdFormatPDF); // word保存为pdf格式宏，值为17
            app.setProperty("DisplayAlerts",true);
            System.out.println("*****转换成功...*****");
            return true; // set flag true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (doc != null) {
                Dispatch.call(doc, "Close", false);
            }
            if (app != null) {
                app.invoke("Quit", 0);
            }

            ComThread.Release();
        }
    }
    public static boolean pptToPdf(String srcFilePath, String pdfFilePath) {
        ActiveXComponent app = null;
        Dispatch ppt = null;
        try {
            ComThread.InitSTA();
            app = new ActiveXComponent("PowerPoint.Application");
            System.out.println("*****正在转换...*****");
            Dispatch ppts = app.getProperty("Presentations").toDispatch();

            // 因POWER.EXE的发布规则为同步，所以设置为同步发布
            ppt = Dispatch.call(ppts, "Open", srcFilePath, true,// ReadOnly
                    true,// Untitled指定文件是否有标题
                    false// WithWindow指定文件是否可见
            ).toDispatch();
            //Dispatch.put(ppt, "CheckCompatibility", false);  //兼容性检查,为特定值false不正确
            app.setProperty("DisplayAlerts",false);
            Dispatch.call(ppt, "SaveAs", pdfFilePath, pptFormatPDF); //ppSaveAsPDF为特定值32
            app.setProperty("DisplayAlerts",true);
            System.out.println("*****转换成功...*****");
            return true; // set flag true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (ppt != null) {
                Dispatch.call(ppt, "Close");
            }
            if (app != null) {
                app.invoke("Quit");
            }
            ComThread.Release();
        }
    }
    public static boolean excelToPDF(String srcFilePath, String pdfFilePath) {
        ActiveXComponent app = null;
        Dispatch excel = null;
        try {

            ComThread.InitSTA(true);
             app = new ActiveXComponent("Excel.Application");
            System.out.println("*****正在转换...*****");
            long date = new Date().getTime();
            app.setProperty("Visible", false);
            app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
            Dispatch excels = app.getProperty("Workbooks").toDispatch();

             excel = Dispatch
                    .invoke(excels, "Open", Dispatch.Method,
                            new Object[] { srcFilePath, new Variant(false), new Variant(false) }, new int[9])
                    .toDispatch();
            // 转换格式
            Dispatch.put(excel, "CheckCompatibility", false);
            app.setProperty("DisplayAlerts",false);
            Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, new Object[] { new Variant(0), // PDF格式=0
                    pdfFilePath, new Variant(xlTypePDF) // 0=标准 (生成的PDF图片不会变模糊) 1=最小文件
                    // (生成的PDF图片糊的一塌糊涂)
            }, new int[1]);
            app.setProperty("DisplayAlerts",true);
            System.out.println("*****转换成功...*****");
            // 这里放弃使用SaveAs
            /*
             * Dispatch.invoke(excel,"SaveAs",Dispatch.Method,new Object[]{
             * outFile, new Variant(57), new Variant(false), new Variant(57),
             * new Variant(57), new Variant(false), new Variant(true), new
             * Variant(57), new Variant(true), new Variant(true), new
             * Variant(true) },new int[1]);
             */

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
            return false;
        }finally {
            if (excel != null) {
                Dispatch.call(excel, "Close", false);
            }
            if (app != null) {
                app.invoke("Quit", new Variant[] {});
                //app.invoke("Quit", 0);
            }
            ComThread.Release();
        }
    }
}
